package com.td.framework.utils;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * Title: 格式化类
 * Description:格式化类
 * Copyright: Copyright (c) 2008
 * Company 北京水软科技有限公司
 *
 * @author
 * @version 1.0
 */
public class Text {

    public Text() {
    }

    /**
     *
     * @param s
     * @return
     */
    public static String getNumberString(String s) {
        String num = "0123456789-.";
        String ret = "";
        if (s != null) {
            for (int i = 0; i < s.length(); i++) {
                String ch = String.valueOf(String.valueOf(s.charAt(i))).concat(
                    "");
                if (num.indexOf(ch) >= 0) {
                    ret = String.valueOf(ret) + String.valueOf(ch);
                }
            }

        }
        return ret;
    }

    /**
     * 把String类型数字,格式化成Double类型
     * @param numberString
     * @return
     */
    public static double parseDouble(String numberString) {
        try {
            double amount = Double.parseDouble(getNumberString(numberString));
            double d1 = amount;
            return d1;
        }
        catch (Exception e) {
            double d = 0.0D;
            return d;
        }
    }

    /**
     *
     * @param amount
     * @return
     */
    public static String formatCurrency(double amount) {
        String ret = formatCurrency0(amount);
        if (ret.equals("0.00")) {
            ret = "";
        }
        return ret;
    }

    /**
     *
     * @param amount
     * @return
     */
    public static String formatCurrency0(double amount) {
        String pattern = "#,###,###,###,##0.00";
        String ret = formatNumber(amount, pattern);
        return ret;
    }

    /**
     *
     * @param amount
     * @return
     */
    public static String formatPlainCurrency(double amount) {
        String pattern = "############0.00";
        String ret = formatNumber(amount, pattern);
        return ret;
    }

    /**
     *
     * @param n
     * @return
     */
    public static String formatInteger(int n) {
        return formatNumber(n, "00000000");
    }

    /**
     *
     * @param n
     * @param pattern
     * @return
     */
    public static String formatInteger(int n, String pattern) {
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        nf.setParseIntegerOnly(true);
        nf.setMaximumFractionDigits(0);
        nf.setGroupingUsed(false);
        return nf.format(n);
    }

    /**
     *
     * @param amount
     * @param pattern
     * @return
     */
    public static String formatNumber(double amount, String pattern) {
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        DecimalFormat df = (DecimalFormat) nf;
        df.setDecimalSeparatorAlwaysShown(true);
        df.applyPattern(pattern);
        return df.format(amount);
    }

    /**
     *
     * @param date
     * @param delimiter
     * @return
     */
    public static String formatDate(Date date, String delimiter) {
        if (date == null) {
            return null;
        }
        else {
            Calendar cl = Calendar.getInstance();
            cl.setTime(date);
            String year = String.valueOf(String.valueOf(cl.get(1))).concat("");
            String month = String.valueOf(String.valueOf( (new StringBuffer(
                String.valueOf(String.valueOf(101 + cl.get(2))))).append("")));
            month = month.substring(1);
            String day = String.valueOf(String.valueOf( (new StringBuffer(
                String.valueOf(String.valueOf(100 + cl.get(5))))).append("")));
            day = day.substring(1);
            return String.valueOf(String.valueOf( (new StringBuffer(String.
                valueOf(String.valueOf(year)))).append(delimiter).append(month).
                                                 append(delimiter).append(day)));
        }
    }

    /**
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        return formatDate(date, "-");
    }

    /**
     *
     * @param date
     * @return
     */
    public static String formatDateTime(Date date) {
        if (date == null) {
            return null;
        }
        else {
            Calendar cl = Calendar.getInstance();
            cl.setTime(date);
            String year = String.valueOf(String.valueOf(cl.get(1))).concat("");
            String month = String.valueOf(String.valueOf( (new StringBuffer(
                String.valueOf(String.valueOf(101 + cl.get(2))))).append("")));
            month = month.substring(1);
            String day = String.valueOf(String.valueOf( (new StringBuffer(
                String.valueOf(String.valueOf(100 + cl.get(5))))).append("")));
            day = day.substring(1);
            String h = String.valueOf(String.valueOf( (new StringBuffer(String.
                valueOf(String.valueOf(cl.get(11) + 100)))).append("")));
            String m = String.valueOf(String.valueOf( (new StringBuffer(String.
                valueOf(String.valueOf(cl.get(12) + 100)))).append("")));
            String s = String.valueOf(String.valueOf( (new StringBuffer(String.
                valueOf(String.valueOf(cl.get(13) + 100)))).append("")));
            String ms = String.valueOf(String.valueOf( (new StringBuffer(String.
                valueOf(String.valueOf(cl.get(14) + 1000)))).append("")));
            return String.valueOf(String.valueOf( (new StringBuffer(String.
                valueOf(String.valueOf(year)))).append("-").append(month).
                                                 append("-").append(day).append(
                " ").append(h.substring(1)).append(":").append(m.substring(1)).
                                                 append(":").append(s.substring(
                1))));
        }
    }
    
    
    public static void main(String[] args) {
    	Date da = new Date();
    	System.out.println("*********************"+da);
    	Date d1 =DateUtils.addDay(da, 1);
    	System.out.println("*********************"+d1);
	}
}
